package com.dot2.mybatisdemo.controller;

import com.dot2.mybatisdemo.dao.BlogMapper;
import com.dot2.mybatisdemo.dao.UserMapper;
import com.dot2.mybatisdemo.entity.User;
import com.dot2.mybatisdemo.util.PageRequest;
import com.dot2.mybatisdemo.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    UserMapper userMapper;

    @Resource
    BlogMapper blogMapper;

    @GetMapping("/users")
    Object users() {
        return userMapper.findAll();
    }

    @PostMapping("/users-pages")
    Object userPages(@RequestBody PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.findAll();
        return PageUtils.getPageResult(new PageInfo<User>(users));
    }

    @GetMapping("/get-users-pages")
    Object userPages(int page, int size) {
        PageHelper.startPage(page, size);
        List<User> users = userMapper.findAll();
        return PageUtils.getPageResult(new PageInfo<User>(users));
    }

    @GetMapping("/blog")
    Object getBlog(int id){
        return blogMapper.selectBlog(id);
    }
}
